package cn.tedu.zhyl.system.controller;

import cn.tedu.zhyl.base.response.JsonResult;
import cn.tedu.zhyl.base.response.PageResponse;
import cn.tedu.zhyl.base.response.StatusCode;
import cn.tedu.zhyl.system.pojo.dto.LoginDTO;
import cn.tedu.zhyl.system.pojo.dto.SysUserParams;
import cn.tedu.zhyl.system.pojo.dto.SysUserQuery;
import cn.tedu.zhyl.system.pojo.vo.LoginVO;
import cn.tedu.zhyl.system.pojo.vo.SysUserVO;
import cn.tedu.zhyl.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/user/")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 新建用户和修改用户
     * @return
     */
    @PostMapping("save")
    public JsonResult save(SysUserParams sysUserParams){
        sysUserService.save(sysUserParams);
        return JsonResult.ok();
    }

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */

    @PostMapping("login")
    public JsonResult<LoginVO> login(@RequestBody LoginDTO loginDTO){
        LoginVO loginVO = sysUserService.login(loginDTO);
        return JsonResult.build(loginVO,StatusCode.SUCCESS_OPERATION);
    }

    /**
     * 退出登录
     * @param token
     * @return
     */

    @PostMapping("logout")
    public JsonResult logout(@RequestHeader(name = "token")String token){
        sysUserService.logout(token);
        return JsonResult.build(null,StatusCode.SUCCESS_OPERATION);
    }


    /**
     * 分页查询用户列表
     * @param page
     * @param limit
     * @param sysUserQuery
     * @return
     */

    @GetMapping("getListByPage/{page}/{limit}")
    public JsonResult<PageResponse<SysUserVO>> findByPage(@PathVariable Integer page,
                                               @PathVariable Integer limit,
                                               SysUserQuery sysUserQuery){
        PageResponse<SysUserVO> list = sysUserService.findByPage(page,limit,sysUserQuery);
        return JsonResult.ok(list);
    }

    /**
     * 更改用户状态
     * @param uid
     * @param status
     * @return
     */
    @GetMapping("update/status/{uid}/{status}")
    public JsonResult updateUserStatus(@PathVariable Long uid,@PathVariable Integer status){
        sysUserService.updateUserStatus(uid,status);
        return JsonResult.ok();
    }

    /**
     * 重置密码
     * @param uid
     * @return
     */
    @GetMapping("reset/{uid}")
    public JsonResult resetPassword(@PathVariable Long uid){
        sysUserService.resetPassword(uid);
        return JsonResult.ok();
    }


    @GetMapping("deleted/{uid}")
    public JsonResult delete(@PathVariable Long uid){
        sysUserService.delete(uid);
        return JsonResult.ok();
    }
}
